package nl.janvanloenen.bkc;

import java.sql.Date;
import java.sql.Time;
import java.util.Calendar;

import nl.janvanloenen.bkc.Match.State;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;

public class NewMatch extends Activity {

	private Button btn_change_date;
	private Button btn_change_start_time;
	private Button btn_change_end_time;
	private Button btn_save;

	private TextView tvDate;
	private TextView tvStartTime;
	private TextView tvEndTime;

	private EditText et_team_a;
	private EditText et_team_b;

	public static final int Date_dialog_id = 1;
	// date and time
	private int mYear;
	private int mMonth;
	private int mDay;
	private int mHour;
	private int mMinute;
	private int mEndHour;
	private int mEndMinute;

	private DatabaseHandler db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_match);
		btn_change_date = (Button) findViewById(R.id.btnSetDate);
		btn_change_start_time = (Button) findViewById(R.id.btnSetStartTime);
		btn_change_end_time = (Button) findViewById(R.id.btnSetEndTime);
		btn_save = (Button) findViewById(R.id.btnSave);
		tvDate = (TextView) findViewById(R.id.tvDate);
		tvStartTime = (TextView) findViewById(R.id.tvStartTime);
		tvEndTime = (TextView) findViewById(R.id.tvEndTime);
		et_team_a = (EditText) findViewById(R.id.editText1);
		et_team_b = (EditText) findViewById(R.id.editText2);

		btn_change_date.setOnClickListener(DateHandler);
		btn_change_start_time.setOnClickListener(timehandler);
		btn_change_end_time.setOnClickListener(endtimehandler);
		btn_save.setOnClickListener(saveToDB);

		// Set current date
		final Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);
		mHour = c.get(Calendar.HOUR_OF_DAY);
		mMinute = c.get(Calendar.MINUTE);
		mEndHour = c.get(Calendar.HOUR_OF_DAY) + 1;
		mEndMinute = mMinute;

		db = new DatabaseHandler(this);

		// set Labels
		UpdateDate();
		UpdateStartTime();
		UpdateEndTime();
	}

	// Date picker
	View.OnClickListener DateHandler = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			DatePickerDialog DPD = new DatePickerDialog(NewMatch.this,
					mDateSetListener, mYear, mMonth, mDay);
			DPD.show();
		}
	};

	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear;
			mDay = dayOfMonth;
			UpdateDate();
		}
	};

	private void UpdateDate() {
		tvDate.setText(new StringBuilder()
				// Month is 0 based so add 1
				.append(mDay).append("-").append(mMonth + 1).append("-")
				.append(mYear));
	}

	// start time picker
	View.OnClickListener timehandler = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			TimePickerDialog TPD = new TimePickerDialog(NewMatch.this,
					TPDListener, mHour, mMinute, true);
			TPD.show();
		}
	};

	private TimePickerDialog.OnTimeSetListener TPDListener = new TimePickerDialog.OnTimeSetListener() {

		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			mHour = hourOfDay;
			mMinute = minute;
			UpdateStartTime();

		}
	};

	private void UpdateStartTime() {
		if (mMinute < 10) {
			tvStartTime.setText(new StringBuilder().append(mHour).append(":0")
					.append(mMinute));
		} else {
			tvStartTime.setText(new StringBuilder().append(mHour).append(":")
					.append(mMinute));
		}
	}

	// end time picker
	View.OnClickListener endtimehandler = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			TimePickerDialog ETPD = new TimePickerDialog(NewMatch.this,
					endTPDListener, mEndHour, mEndMinute, true);
			ETPD.show();
		}
	};

	private TimePickerDialog.OnTimeSetListener endTPDListener = new TimePickerDialog.OnTimeSetListener() {

		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			mEndHour = hourOfDay;
			mEndMinute = minute;
			UpdateEndTime();

		}
	};

	private void UpdateEndTime() {
		if (mEndMinute < 10) {
			tvEndTime.setText(new StringBuilder().append(mEndHour).append(":0")
					.append(mEndMinute));
		} else {

			tvEndTime.setText(new StringBuilder().append(mEndHour).append(":")
					.append(mEndMinute));
		}
	}

	// save button
	View.OnClickListener saveToDB = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			String team1 = et_team_a.getText().toString();
			String team2 = et_team_b.getText().toString();
			System.out.println("year : " + mYear);
			Date date = new Date(mYear - 1900, mMonth, mDay);
			System.out.println("date : " + date.toString());
			Time startTime = new Time(mHour, mMinute, 0);
			Time endTime = new Time(mEndHour, mEndMinute, 0);
			startTime.toString();
			db.addMatch(new Match(team1, team2, date.toString(), startTime
					.getTime(), endTime.getTime(), 0, 0, State.NEW.toString()));
			db.close();
			stopActivity();
		}
	};

	private void stopActivity() {
		finish();
	}
}
